package wnhz.com.business.shareshop.adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.RecyclerViewHolder;
import wnhz.com.business.groupshop.bean.OrderBean;
import wnhz.com.business.impl.ITaskFinishListener;
import wnhz.com.business.shareshop.activity.mine.OrderDetailsActivity;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.MyRecyclerView;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class FragmentAllAdapter extends BaseRecyclerAdapter<OrderBean.InfoBean> {
    static final String TAG = "CollectAdapter";


    private MyItemClickLinstener ClickLinstener;
//    private ProgressDialog mProgressDialog;
    private OrderBean orderBean;
    private List<OrderBean.InfoBean> infoList;

    private String copewith_price;

    private int paging;

    public void setPaging(int paging) {
        this.paging = paging;
    }

    public FragmentAllAdapter(Context context, List<OrderBean.InfoBean> datas) {
        super(context, datas, R.layout.item_all_layout);
    }

    public void setOnClickListener(MyItemClickLinstener listener) {
        ClickLinstener = listener;
    }

    @Override
    protected void loadData(int page, final ITaskFinishListener iTaskFinishListener) {
//        if (mProgressDialog == null) {
//            mProgressDialog = new ProgressDialog(context);
//            mProgressDialog.setCancelable(true);
//            mProgressDialog.setCanceledOnTouchOutside(false);
//        }
//        mProgressDialog.setMessage("加载中...");
//        mProgressDialog.show();

        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getToken());
        map.put("page",paging);
        map.put("goods_type",4);
        /**
         我的订单
         @param str           token
         @param Int           type      不传type为全部订单  1-待付款 2-待发货 3-已发货 4-已完成 5-待评价 6退款中 11待成团
         @param page          分页查询
         @param goods_type    1.拼团  2.返利  3.免费   4：共享商城
         */
        XUtil.Post(Url.ORDER_MYORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("----------全部订单-----", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    String url = jsonObject.optString("url");
                    if (re.equals("1")) {
                        Gson gson = new Gson();
                        orderBean = gson.fromJson(result, OrderBean.class);
                        infoList = orderBean.getInfo();
                        iTaskFinishListener.onSuccess(TAG, infoList);

                    } else if (re.equals("0")) {
                        iTaskFinishListener.onSuccess(TAG, infoList);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
//                    if (mProgressDialog != null) {
//                        mProgressDialog.dismiss();
//                    }
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
//                if (mProgressDialog != null) {
//                    mProgressDialog.dismiss();
//                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
//                if (mProgressDialog != null) {
//                    mProgressDialog.dismiss();
//                }
            }
        });
    }

    @Override
    protected void bindViewHolder(int position, RecyclerViewHolder holder) {
        TextView tv_cancle = holder.getView(R.id.tv_cancle, false);   //取消订单
        TextView tv_pay = holder.getView(R.id.tv_pay, false);         //付款
        TextView tv_sure = holder.getView(R.id.tv_sure, false);       //确认收货
        TextView tv_pingjia_go = holder.getView(R.id.tv_pingjia_go, false);       //去评价
        TextView tv_delete = holder.getView(R.id.tv_delete, false);       //删除订单
        TextView tv_remind = holder.getView(R.id.tv_remind, false);  //提醒发货
        TextView tv_logistics = holder.getView(R.id.tv_logistics, false);  //查看物流
        TextView tv_drawback = holder.getView(R.id.tv_drawback, false);  //申请退款

        TextView tv_order_haoma = holder.getView(R.id.tv_order_haoma, false);

        TextView tv_amountprice = holder.getView(R.id.tv_amountprice, false);
        TextView tv_amount = holder.getView(R.id.tv_amount, false);
        TextView tv_order_zhuangtai = holder.getView(R.id.tv_order_zhuangtai, false);

        LinearLayout tv_order_item = holder.getView(R.id.tv_order_item, false);
        MyRecyclerView recyclerView = holder.getView(R.id.recycler_inner, false);
        recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        final OrderBean.InfoBean item = getItem(position);
        final List<OrderBean.InfoBean.GoodsBean> listOrder = item.getGoods();
        recyclerView.setAdapter(new OrderItemAdapter(context,listOrder));

//        recyclerView.setAdapter(new BaseRVAdapter(context, listOrder) {
//            @Override
//            public int getLayoutId(int viewType) {
//                return R.layout.item_one_item_layout;
//            }
//
//            @Override
//            public void onBind(BaseViewHolder holder, int position) {
//                ImageView img = holder.getView(R.id.iv_img);
//                Glide.with(context).load(listOrder.get(position).getGoods_pic()).into(img);
//                img.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        context.startActivity(OrderDetailsActivity.createIntent(context, item.getO_id()));
//                    }
//                });
//            }
//        });
//
//        tv_order_item.setOnClickListener(new View.OnClickListener() { //订单详情
//            @Override
//            public void onClick(View v) {
//                LogUtils.e("-----------点击一条----", "-------------点击一条----");
//                context.startActivity(OrderDetailsActivity.createIntent(context, item.getO_id()));
//            }
//        });
        //订单状态（0：已取消，1：待付款,2：待发货，3：已发货，4：已完成，5：已关闭，6：退款中， 7：订单删除   8:订单退货中  ,9：返仓中，10：已返仓）',

        if (item.getOrder_status().equals("0")) {  //已取消        1：待付款,2：待发货，3：待收货，4：已完成
            tv_order_zhuangtai.setText("订单已取消");
            tv_delete.setVisibility(View.VISIBLE);
            tv_cancle.setVisibility(View.GONE);
            tv_pay.setVisibility(View.GONE);
            tv_remind.setVisibility(View.GONE);
            tv_logistics.setVisibility(View.GONE);
            tv_sure.setVisibility(View.GONE);
            tv_pingjia_go.setVisibility(View.GONE);
            tv_drawback.setVisibility(View.GONE);
        } else if (item.getOrder_status().equals("1")) {  //待付款        1：待付款,2：待发货，3：待收货，4：已完成
            tv_order_zhuangtai.setText("待付款");
            tv_cancle.setVisibility(View.VISIBLE);
            tv_pay.setVisibility(View.VISIBLE);
            tv_remind.setVisibility(View.GONE);
            tv_logistics.setVisibility(View.GONE);
            tv_sure.setVisibility(View.GONE);
            tv_delete.setVisibility(View.GONE);
            tv_pingjia_go.setVisibility(View.GONE);
            tv_drawback.setVisibility(View.GONE);
        } else if (item.getOrder_status().equals("2")) {   //待发货
            tv_order_zhuangtai.setText("待发货");
            tv_remind.setVisibility(View.VISIBLE);
            tv_drawback.setVisibility(View.VISIBLE);
            tv_logistics.setVisibility(View.GONE);
            tv_sure.setVisibility(View.GONE);
            tv_delete.setVisibility(View.GONE);
            tv_pingjia_go.setVisibility(View.GONE);
            tv_cancle.setVisibility(View.GONE);
            tv_pay.setVisibility(View.GONE);
        } else if (item.getOrder_status().equals("3")) {   //待收货
            tv_order_zhuangtai.setText("待收货");
            tv_logistics.setVisibility(View.VISIBLE);
            tv_sure.setVisibility(View.VISIBLE);
            tv_delete.setVisibility(View.GONE);
            tv_pingjia_go.setVisibility(View.GONE);
            tv_cancle.setVisibility(View.GONE);
            tv_pay.setVisibility(View.GONE);
            tv_remind.setVisibility(View.GONE);
            tv_drawback.setVisibility(View.GONE);
        } else if (item.getOrder_status().equals("4")) {
            if (item.getEvaluate_type().equals("0")) {
                //未评价
                tv_order_zhuangtai.setText("未评价");
                tv_logistics.setVisibility(View.GONE);
                tv_pingjia_go.setVisibility(View.VISIBLE);
                tv_delete.setVisibility(View.GONE);
                tv_cancle.setVisibility(View.GONE);
                tv_pay.setVisibility(View.GONE);
                tv_sure.setVisibility(View.GONE);
                tv_remind.setVisibility(View.GONE);
                tv_drawback.setVisibility(View.GONE);
            } else if (item.getEvaluate_type().equals("1")) {
                //已评价
                if (item.getOrder_status().equals("4")) {    //已完成
                    tv_order_zhuangtai.setText("已完成");
                    tv_logistics.setVisibility(View.VISIBLE);
                    tv_cancle.setVisibility(View.GONE);
                    tv_pay.setVisibility(View.GONE);
                    tv_delete.setVisibility(View.GONE);
                    tv_pingjia_go.setVisibility(View.GONE);
                    tv_sure.setVisibility(View.GONE);
                    tv_remind.setVisibility(View.GONE);
                    tv_drawback.setVisibility(View.GONE);
                }
            }
        } else if (item.getOrder_status().equals("5")) {  //已关闭
            tv_order_zhuangtai.setText("订单已关闭");
            tv_cancle.setVisibility(View.GONE);
            tv_pay.setVisibility(View.GONE);
            tv_delete.setVisibility(View.GONE);
            tv_pingjia_go.setVisibility(View.GONE);
            tv_sure.setVisibility(View.GONE);
            tv_remind.setVisibility(View.GONE);
            tv_logistics.setVisibility(View.GONE);
            tv_drawback.setVisibility(View.GONE);
        } else if (item.getOrder_status().equals("6")) {  //退款中
            tv_order_zhuangtai.setText("退款中");
            tv_cancle.setVisibility(View.GONE);
            tv_pay.setVisibility(View.GONE);
            tv_delete.setVisibility(View.GONE);
            tv_pingjia_go.setVisibility(View.GONE);
            tv_sure.setVisibility(View.GONE);
            tv_remind.setVisibility(View.GONE);
            tv_logistics.setVisibility(View.GONE);
            tv_drawback.setVisibility(View.GONE);
        }

        tv_amount.setText("共" + item.getGoods().size() + "件商品  合计:");
        tv_amountprice.setText("¥ " + item.getCopewith_price());
        tv_order_haoma.setText("订单号:" + item.getOrder_no());

        if (position < orderBean.getInfo().size()) {
            //去支付的应付金额
            copewith_price = orderBean.getInfo().get(position).getCopewith_price();

            tv_cancle.setTag(R.id.tv_cancle, position);
            tv_cancle.setOnClickListener(itemClickLinstener);

            tv_delete.setTag(R.id.tv_delete, position);
            tv_delete.setOnClickListener(itemClickLinstener);

            tv_pay.setTag(R.id.tv_pay, position);
            tv_pay.setOnClickListener(itemClickLinstener);

            tv_pingjia_go.setTag(R.id.tv_pingjia_go, position);
            tv_pingjia_go.setOnClickListener(itemClickLinstener);

            tv_sure.setTag(R.id.tv_sure, position);
            tv_sure.setOnClickListener(itemClickLinstener);

            tv_remind.setTag(R.id.tv_remind, position);
            tv_remind.setOnClickListener(itemClickLinstener);

            tv_logistics.setTag(R.id.tv_logistics, position);
            tv_logistics.setOnClickListener(itemClickLinstener);

            tv_drawback.setTag(R.id.tv_drawback, position);
            tv_drawback.setOnClickListener(itemClickLinstener);
        }

    }

    @Override
    protected void setPageSize(int pageSize) {
    }

    private View.OnClickListener itemClickLinstener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ClickLinstener != null) {
                int position;
                switch (v.getId()) {
                    case R.id.tv_cancle:
                        position = (int) v.getTag(R.id.tv_cancle);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnCancleClickListener(FragmentAllAdapter.this, position, v);
                        break;
                    case R.id.tv_delete:
                        position = (int) v.getTag(R.id.tv_delete);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnDeleteClickListener(FragmentAllAdapter.this, position, v);
                        break;
                    case R.id.tv_pay:
                        position = (int) v.getTag(R.id.tv_pay);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnPayClickListener(FragmentAllAdapter.this, position, copewith_price, v);
                        break;
                    case R.id.tv_pingjia_go:
                        position = (int) v.getTag(R.id.tv_pingjia_go);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnPingJiaClickListener(FragmentAllAdapter.this, position, v);
                        break;
                    case R.id.tv_sure:
                        position = (int) v.getTag(R.id.tv_sure);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnSureClickListener(FragmentAllAdapter.this, position, v);
                        break;
                    case R.id.tv_remind:
                        position = (int) v.getTag(R.id.tv_remind);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnRemindClickListener(FragmentAllAdapter.this, position, v);
                        break;
                    case R.id.tv_logistics:
                        position = (int) v.getTag(R.id.tv_logistics);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnLogisticsClickListener(FragmentAllAdapter.this, position, v);
                        break;
                    case R.id.tv_drawback:
                        position = (int) v.getTag(R.id.tv_drawback);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnDrawbackClickListener(FragmentAllAdapter.this, position, v);
                        break;
                }
            }
        }
    };


    public interface MyItemClickLinstener {
        void OnCancleClickListener(FragmentAllAdapter adapter, int position, View view);

        void OnPayClickListener(FragmentAllAdapter adapter, int position, String money, View view);

        void OnSureClickListener(FragmentAllAdapter adapter, int position, View view);

        void OnDeleteClickListener(FragmentAllAdapter adapter, int position, View view);

        void OnPingJiaClickListener(FragmentAllAdapter adapter, int position, View view);

        void OnRemindClickListener(FragmentAllAdapter adapter, int position, View view);

        void OnLogisticsClickListener(FragmentAllAdapter adapter, int position, View view);

        void OnDrawbackClickListener(FragmentAllAdapter adapter, int position, View view);

    }
}
